package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.Course;
import com.ruoyi.common.core.domain.entity.Smalluser;
import com.ruoyi.common.core.domain.entity.Student;
import com.ruoyi.system.mapper.InstitutionMapper;
import com.ruoyi.system.mapper.InstitutionStudentMapper;
import com.ruoyi.system.mapper.InstitutionUserMapper;
import com.ruoyi.system.mapper.SysCourseMapper;
import com.ruoyi.system.service.ISysCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程管理模块
 * @author Ander.Zhao
 * @date 2021-05-11
 */
@Service
public class SysSysCourseServiceImpl implements ISysCourseService {
    @Autowired
    private SysCourseMapper sysCourseMapper;

    @Autowired
    private InstitutionUserMapper institutionUserMapper;
    @Autowired
    private InstitutionStudentMapper institutionStudentMapper;
    /**
     * 查询所有课程
     * @return
     */
    @Override
    public List<Course> selectAll(Course course) {
        return sysCourseMapper.selectCourse(course);
    }

    /**
     * 根据分类编号查询课程
     * @param typeid 课程分类编号
     * @return
     */
    @Override
    public List<Course> selectByTid(long typeid) {
        return     sysCourseMapper.selectCourseByTypeid(typeid);
    }

    /**
     * 根据课程编号查询课程
     * @param courseid 课程编号
     * @return
     */
    @Override
    public Course selectById(long courseid) {
        return sysCourseMapper.selectCourseById(courseid);
    }

    /**
     * 删除课程
     * @param courseids 课程编号
     * @return
     */
    @Override
    public int delect(Long[] courseids) {

        for (long courseid: courseids) {
            sysCourseMapper.removeCourse(courseid);
            sysCourseMapper.courseGradeDelete(courseid);
        }
        return courseids.length;
    }

    @Override
    public int add(Course course) {
      // courseMapper.addCourseGrade(course);
        return sysCourseMapper.addCourse(course);
    }

    @Override
    public int update(Course course) {
        return sysCourseMapper.updateCourse(course);
    }

    @Override
    public List<Smalluser> selectUser(Long itId) {
        List<Smalluser> smallusers = institutionUserMapper.selectUserList(itId);
        List<Smalluser> res=new ArrayList<>();
        for ( Smalluser user: smallusers) {
            Student stu=new Student();
            stu.setSuId(user.getSuId());
            List<Student> students = institutionStudentMapper.selectStudentList(stu);
            user.setChildren(students);
            res.add(user);
        }
        return  res;
    }


}
